
/*遥控器的处理，以及键鼠输入的所有的字母，注意：键盘输入时可以是同时按两个键*/
#include "remote.h"
#include "chassis.h"
#include "sys.h"
#include "motorhead.h"
#include "tim.h"
#include "ramp.h"
#include "test_app.h"
#include "can.h"
#include "referee_system.h"
#include "pid.h"
#include "config.h"

#if CHASSIS_FEEDBACK != 0 //限制功率
#define NORMAL_FORWARD_BACK_SPEED 5000
#define NORMAL_LEFT_RIGHT_SPEED 5000

#define HIGH_FORWARD_BACK_SPEED 5500
#define HIGH_LEFT_RIGHT_SPEED 5500
#else
#define NORMAL_FORWARD_BACK_SPEED 5500
#define NORMAL_LEFT_RIGHT_SPEED 5500

#define HIGH_FORWARD_BACK_SPEED 6500
#define HIGH_LEFT_RIGHT_SPEED 6500
#endif

RC_Ctl_t RC_CtrlData = {0}; //remote control data
Gimbal_Ref_t GimbalRef = {0};
REFEREE_LIMIT_MESSAGE refree_change_inf = {0};
OPERATE_MESSAGE key_mouse_inf = {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0};
ShootingFreCon_t ShootingFreCon = {1, 0, 0, 90, 90, 1.8};
FrictionWheelState_e friction_wheel_state = FRICTION_WHEEL_OFF;
float volatile yawref_angle_last = 0.2;
float distance;
int q_count = 0, e_count = 0;
u8 i = 0;
u8 PTZ_static_flag = 1;
u8 PTZ_save_flag = 1;
u8 start_stasticflag = 1;
u8 level = 1;
int up_count;
int down_count;
volatile float angle_offset = 0;

static InputMode_e inputmode = REMOTE_INPUT; //输入模式设定
u8 flag_shoot = 0;							 //记录摩擦轮的状态，控制拨弹电机能否转动
uint8_t RxMsg[18];
int remote[30];

void MouseShootControl(Mouse *mouse)
{
	static int16_t closeDelayCount = 0; //右键关闭摩擦轮3s延时计数

	switch (friction_wheel_state)
	{
	case FRICTION_WHEEL_OFF:
	{
		if (mouse->last_press_r == 0 && mouse->press_r == 1) //从关闭到start turning
		{

			TIM_SetTIM12Compare1(1000);
			TIM_SetTIM12Compare2(1000);
			friction_wheel_state = FRICTION_WHEEL_START_TURNNING;
			closeDelayCount = 0;
		}
		//			LASER_OFF();
	}
	break;

	case FRICTION_WHEEL_START_TURNNING:
	{
		if (mouse->press_r == 1)
		{
			closeDelayCount++;
		}
		else
		{
			closeDelayCount = 0;
		}

		if (closeDelayCount > FRICITION_CLOSE_DELAY) //关闭摩擦轮
		{
			friction_wheel_state = FRICTION_WHEEL_OFF;
			/*
							typedef enum
							{
								FRICTION_WHEEL_OFF = 0,  //摩擦轮关闭
								FRICTION_WHEEL_START_TURNNING = 1,  //摩擦轮开始旋转
								FRICTION_WHEEL_ON = 2,   //摩擦轮在运行
							}FrictionWheelState_e;
				*/

			TIM_SetTIM12Compare1(1000);
			TIM_SetTIM12Compare2(1000);
		}
		else
		{
			/*摩擦轮加     refree_change_inf.fricition_speed=LOW_FRICITION_ON_SPEED; LOW_FRICITION_ON_SPEED    1440   //大火力摩擦轮基础速度(Vave=18/s)*/
			TIM_SetTIM12Compare1(refree_change_inf.fricition_speed);
			TIM_SetTIM12Compare2(refree_change_inf.fricition_speed);
			friction_wheel_state = FRICTION_WHEEL_ON;
			//				    LASER_ON();
		}
	}
	break;

	case FRICTION_WHEEL_ON:
	{

		TIM_SetTIM12Compare1(refree_change_inf.fricition_speed);
		TIM_SetTIM12Compare2(refree_change_inf.fricition_speed);
		if (mouse->press_r == 1)
		{
			closeDelayCount++;
		}
		else
		{
			closeDelayCount = 0;
		}

		if (closeDelayCount > FRICITION_CLOSE_DELAY) //关闭摩擦轮
		{
			friction_wheel_state = FRICTION_WHEEL_OFF;

			TIM_SetTIM12Compare1(1000);
			TIM_SetTIM12Compare2(1000);
		}
		else if (mouse->press_l == 1 && mouse->last_press_l == 0) /*按下左键，射击 单次触发*/
		{

#if DIALPOSITIONORSPEED == 1 /*拨弹电机位置环*/
			distance = __fabs(Dial_the_motor.ecd_angle - position_hit);

			if (key_mouse_inf.bursts_flag) //如果为单发
			{
#if SHOOT_FEEDBACK == 1 /*位置环+裁判系统*/
				if (PowerHeatData.heat_count > PowerHeatData.heat_count_last + 3)
				{
					if (max_heat - PowerHeatData.shooterHeat0 > refree_change_inf.one_bullet_heat && distance < refree_change_inf.one_bullet_dis)
					{
						position_hit += DIAL_ONE_POSITION;
						key_mouse_inf.shoot_flag = 1;
						PowerHeatData.heat_count_last = PowerHeatData.heat_count;
					}
				}
#elif SHOOT_FEEDBACK == 2 || SHOOT_FEEDBACK == 3 /*位置环+电流计*/
				if (Get_Time5_Micros() >= ShootingFreCon.LastcountTime + 10000)
				{
					ShootingFreCon.remain_heat += (Get_Time5_Micros() - ShootingFreCon.LastcountTime) / 10000.0 * ShootingFreCon.cooling_heat;
					ShootingFreCon.LastcountTime = Get_Time5_Micros();

					if (ShootingFreCon.remain_heat > ShootingFreCon.limit_heat)
						ShootingFreCon.remain_heat = ShootingFreCon.limit_heat;
				}

				if (ShootingFreCon.remain_heat > refree_change_inf.one_bullet_heat)
				{
					ShootingFreCon.Shooting_Flag = 1;
				}

				if (ShootingFreCon.Shooting_Flag)
				{
					if (distance < refree_change_inf.one_bullet_dis)
					{
						position_hit += DIAL_ONE_POSITION;
						key_mouse_inf.shoot_flag = 1;
						ShootingFreCon.Shooting_Flag = 0;
						ShootingFreCon.remain_heat -= refree_change_inf.one_bullet_heat;

						if (ShootingFreCon.remain_heat < 0)
							ShootingFreCon.remain_heat = 0;
					}
				}

#else  /*位置环+无裁判系统*/
				if (distance < ONEBULLET_DISTANCE)
				{
					position_hit += DIAL_ONE_POSITION;
					key_mouse_inf.shoot_flag = 1;
				}
#endif //裁判系统

			} /* 起始于126行的if */

			else /*如果为三连发*/
			{
#if SHOOT_FEEDBACK == 1
				if (PowerHeatData.heat_count > PowerHeatData.heat_count_last + 3)
				{
					if (max_heat - PowerHeatData.shooterHeat0 > refree_change_inf.three_bullet_heat && distance < refree_change_inf.three_bullet_dis)
					{
						position_hit += DIAL_THREE_POSITION;
						key_mouse_inf.shoot_flag = 1;
						PowerHeatData.heat_count_last = PowerHeatData.heat_count;
					}
				}
#elif SHOOT_FEEDBACK == 2 || SHOOT_FEEDBACK == 3
				if (Get_Time5_Micros() >= ShootingFreCon.LastcountTime + 10000)
				{
					ShootingFreCon.remain_heat += (Get_Time5_Micros() - ShootingFreCon.LastcountTime) / 10000.0 * ShootingFreCon.cooling_heat;
					ShootingFreCon.LastcountTime = Get_Time5_Micros();
					if (ShootingFreCon.remain_heat > ShootingFreCon.limit_heat)
						ShootingFreCon.remain_heat = ShootingFreCon.limit_heat;
				}

				if (ShootingFreCon.remain_heat > refree_change_inf.three_bullet_heat)
				{
					ShootingFreCon.Shooting_Flag = 1;
				}

				if (ShootingFreCon.Shooting_Flag)
				{
					if (distance < refree_change_inf.three_bullet_dis)
					{
						position_hit += DIAL_THREE_POSITION;
						key_mouse_inf.shoot_flag = 1;
						ShootingFreCon.Shooting_Flag = 0;
						ShootingFreCon.remain_heat -= refree_change_inf.three_bullet_heat;

						if (ShootingFreCon.remain_heat < 0)
							ShootingFreCon.remain_heat = 0;
					}
				}

#else
				if (distance < THREEBULLET_DISTANCE)
				{
					position_hit += DIAL_THREE_POSITION;
					key_mouse_inf.shoot_flag = 1;
				}
#endif
			}

#else					/*速度环*/
			if (key_mouse_inf.bursts_flag) //如果为单发
			{
#if SHOOT_FEEDBACK == 1 //速度环+裁判系统
				if (max_heat - PowerHeatData.shooterHeat0 > refree_change_inf.one_bullet_heat && ShootingFreCon.Shooting_delay == 0)
				{
					ShootingFreCon.Shooting_delay = DIAL_ONE_DELAY;
					Dial_motor_speed_ref = DIAL_SPEED;
				}

#elif SHOOT_FEEDBACK == 2 || SHOOT_FEEDBACK == 3
				if (Get_Time5_Micros() >= ShootingFreCon.LastcountTime + 10000)
				{
					ShootingFreCon.remain_heat += (Get_Time5_Micros() - ShootingFreCon.LastcountTime) / 10000 * ShootingFreCon.cooling_heat;
					ShootingFreCon.LastcountTime = Get_Time5_Micros();
					if (ShootingFreCon.remain_heat > ShootingFreCon.limit_heat)
						ShootingFreCon.remain_heat = ShootingFreCon.limit_heat;
				}
				if (ShootingFreCon.remain_heat > refree_change_inf.one_bullet_heat)
				{
					ShootingFreCon.Shooting_Flag = 1;
				}
				if (ShootingFreCon.Shooting_Flag)
				{
					ShootingFreCon.Shooting_delay = DIAL_ONE_DELAY;
					Dial_motor_speed_ref = DIAL_SPEED;
					ShootingFreCon.Shooting_Flag = 0;
					ShootingFreCon.remain_heat -= refree_change_inf.one_bullet_heat;

					if (ShootingFreCon.remain_heat < 0)
						ShootingFreCon.remain_heat = 0;
				}

#else
				if (ShootingFreCon.Shooting_delay == 0)
				{
					ShootingFreCon.Shooting_delay = DIAL_ONE_DELAY;
					Dial_motor_speed_ref = DIAL_SPEED;
				}
#endif
			}
			else
			{

#if SHOOT_FEEDBACK == 1 //速度环+裁判系统
				if (max_heat - PowerHeatData.shooterHeat0 > refree_change_inf.three_bullet_heat && ShootingFreCon.Shooting_delay == 0)
				{
					ShootingFreCon.Shooting_delay = DIAL_THREE_DELAY;
					Dial_motor_speed_ref = DIAL_SPEED;
				}
#elif SHOOT_FEEDBACK == 2 || SHOOT_FEEDBACK == 3

				if (Get_Time5_Micros() >= ShootingFreCon.LastcountTime + 10000)
				{
					ShootingFreCon.remain_heat += (Get_Time5_Micros() - ShootingFreCon.LastcountTime) / 10000 * ShootingFreCon.cooling_heat;
					ShootingFreCon.LastcountTime = Get_Time5_Micros();

					if (ShootingFreCon.remain_heat > ShootingFreCon.limit_heat)
						ShootingFreCon.remain_heat = ShootingFreCon.limit_heat;
				}

				if (ShootingFreCon.remain_heat > refree_change_inf.three_bullet_heat)
				{
					ShootingFreCon.Shooting_Flag = 1;
				}

				if (ShootingFreCon.Shooting_Flag)
				{
					ShootingFreCon.Shooting_delay = DIAL_THREE_DELAY;
					Dial_motor_speed_ref = DIAL_SPEED;
					ShootingFreCon.Shooting_Flag = 0;
					ShootingFreCon.remain_heat -= refree_change_inf.three_bullet_heat;

					if (ShootingFreCon.remain_heat < 0)
						ShootingFreCon.remain_heat = 0;
				}
#else
				if (ShootingFreCon.Shooting_delay == 0)
				{
					ShootingFreCon.Shooting_delay = DIAL_THREE_DELAY;
					Dial_motor_speed_ref = DIAL_SPEED;
				}
#endif
			}
#endif
		} /* 起始于120行 */

		else
		{

#if DIALPOSITIONORSPEED == 1
//				     if(Hit_position.pid_out>320&&Dial_motor_speed_fdb<REDIAL_BULLET_SPEED&&key_mouse_inf.shoot_flag)
//					 {
//								 position_hit=Dial_the_motor.ecd_angle-DIAL_BACK_POSITION;
//								 key_mouse_inf.shoot_flag=0;
//					 }
#else

			if (__fabs(Dial_motor_speed_fdb) <= 4 && Hit_speed.pid_out > 500)
			{
				ShootingFreCon.Shooting_delay = 3;
				Dial_motor_speed_ref = -DIAL_SPEED;
			}

			if (ShootingFreCon.Shooting_delay)
			{
				ShootingFreCon.Shooting_delay--;
			}
			else
			{
				Dial_motor_speed_ref = 0;
			}

#endif
		}
	}
	break;

	} /* switch的结束点 */

	mouse->last_press_r = mouse->press_r;
	if (key_mouse_inf.shift_flag == 0) //如果shift按键没有按下
		mouse->last_press_l = mouse->press_l;
	else
		mouse->last_press_l = 0;
}

void MouseKeyControlProcess(Mouse *mouse, Key *key)
{

	if (key->v & 0x10) //shift
	{
		refree_change_inf.fricition_speed = LOW_FRICITION_ON_SPEED;
		refree_change_inf.one_bullet_heat = SLOW_ONEBULLET_HEAT;
		/*
						SLOW_ONEBULLET_HEAT          19   //一颗低速子弹的热量    
						FAST_ONEBULLET_HEAT          27   //一颗高速子弹的热量  
		        SLOW_THREEBULLET_HEAT        57   //三颗低速子弹的热量   
		 		    FAST_THREEBULLET_HEAT        81   //三颗高速子弹的热量   
			 */
		refree_change_inf.three_bullet_heat = SLOW_THREEBULLET_HEAT;
		key_mouse_inf.shift_flag = 1;

		//#if DIALPOSITIONORSPEED==1
		//				if(key_mouse_inf.bursts_flag)
		//				{
		//						Hit_position.Kp = ONE_KP+0.5f;
		//				}
		//
		//				else
		//				{
		//	          Hit_position.Kp =THREE_KP+0.2f;
		//				}
		//#endif
		//
		//		 }
		//	   else
		//		 {
		//		   refree_change_inf.fricition_speed=NORMAL_FRICITION_ON_SPEED;
		//			 refree_change_inf.one_bullet_heat=FAST_ONEBULLET_HEAT;
		//			 refree_change_inf.three_bullet_heat=FAST_THREEBULLET_HEAT;
		//			 key_mouse_inf.shift_flag=0;
		//
		//#if DIALPOSITIONORSPEED==1
		//				if(key_mouse_inf.bursts_flag)
		//				{
		//						Hit_position.Kp = ONE_KP;
		//				}
		//
		//				else
		//				{
		//	          Hit_position.Kp =THREE_KP;
		//				}
		//#endif
	}

	//movement process
	if (key->v & 0x01) // key: w
	{
		ramp = CHASSIS_RAMP_FB;
		if (key_mouse_inf.FB_flag == 0) //是不是重新按下
		{
			ResetSlope(ramp);
			key_mouse_inf.FB_flag = 1;
		}

		Chassis_Speed_Ref.forward_back_ref = NORMAL_FORWARD_BACK_SPEED * Slope(ACCELERA_TIME, ramp);
		key_mouse_inf.play_flag = 0;
	}
	else if (key->v & 0x02) //key: s
	{
		ramp = CHASSIS_RAMP_FB;
		if (key_mouse_inf.play_flag == 0)
		{
			ResetSlope(ramp);
			key_mouse_inf.play_flag = 1;
		}

		key_mouse_inf.FB_flag = 0;
		Chassis_Speed_Ref.forward_back_ref = -NORMAL_FORWARD_BACK_SPEED * Slope(ACCELERA_TIME, ramp);
	}
	else
	{
		ramp = CHASSIS_RAMP_FB;
		ResetSlope(ramp);
		Chassis_Speed_Ref.forward_back_ref = 0;
	}

	if (key->v & 0x04) // key: d
	{
		ramp = CHASSIS_RAMP_RL;
		if (key_mouse_inf.rl_flag == 0)
		{
			ResetSlope(ramp);
			key_mouse_inf.rl_flag = 1;
		}

		Chassis_Speed_Ref.left_right_ref = -NORMAL_LEFT_RIGHT_SPEED * Slope(ACCELERA_TIME, ramp);
		key_mouse_inf.rr_flag = 0;
		//			printf("d\r\n");
	}
	else if (key->v & 0x08) //key: a
	{
		ramp = CHASSIS_RAMP_RL;
		if (key_mouse_inf.rr_flag == 0)
		{
			ResetSlope(ramp);
			key_mouse_inf.rr_flag = 1;
		}

		Chassis_Speed_Ref.left_right_ref = NORMAL_LEFT_RIGHT_SPEED * Slope(ACCELERA_TIME, ramp);
		key_mouse_inf.rl_flag = 0;
	}
	else
	{
		ramp = CHASSIS_RAMP_RL;
		ResetSlope(ramp);
		Chassis_Speed_Ref.left_right_ref = 0;
	}

	if (key->v & 0x20) //ctrl
	{
		key_mouse_inf.ctrl_flag = 1;
	}
	else
	{

		key_mouse_inf.ctrl_flag = 0;
	}

	if (key->v & 0x40) //q
	{
		//连发按键
		q_count++;
		if (q_count > KEY_PRESS_DELAY)
		{
			key_mouse_inf.bursts_flag = !key_mouse_inf.bursts_flag;
			q_count = -250;

#if DIALPOSITIONORSPEED == 1
//				if(key_mouse_inf.bursts_flag)
//				{
//						Hit_position.Kp = ONE_KP;
//					  Hit_position.Kd = ONE_KD;
//				}
//
//				else
//				{
//            Hit_position.Kd =THREE_KD;
//	          Hit_position.Kp =THREE_KP;
//				}
#endif
		}
	}
	else
		q_count = 0;

	if (key->v & 0x80) //  e
	{

		//扭腰按键
		e_count++;
		if (e_count > KEY_PRESS_DELAY)
		{
			key_mouse_inf.save_flag = !key_mouse_inf.save_flag;
			key_mouse_inf.no_twisted = !key_mouse_inf.no_twisted;
			e_count = -250;
		}
	}
	else
		e_count = 0;

#if SHOOT_FEEDBACK == 2									 /*  SHOOT_FEEDBACK      1     //0 不启用热量闭环   1启用裁判系统(热量与等级)    2 定时器计算  3裁判系统(只是读取等级)   */
	if (key->v & 0x10 && key->v & 0x20 && key->v & 0x01) /*ctrl+shift+w */
	{

		up_count++;
		if (up_count > 40)
		{
			level++;
			up_count = -500;
			if (level >= 3)
			{
				level = 3;
			}
		}
	}
	else
		up_count = 0;

	if (key->v & 0x10 && key->v & 0x20 && key->v & 0x02) //ctrl+shift+s
	{

		down_count++;
		if (down_count > 40)
		{
			level--;
			down_count = -500;
			if (level <= 0)
			{
				level = 0;
			}
		}
	}

	else
		down_count = 0;

#endif
	//step2: gimbal ref calc

	VAL_LIMIT(mouse->x, -120, 120);
	VAL_LIMIT(mouse->y, -120, 120);

	GimbalRef.pitch_angle_dynamic_ref -= mouse->y * MOUSE_TO_PITCH_ANGLE_INC_FACT;
	GimbalRef.yaw_angle_dynamic_ref += mouse->x * MOUSE_TO_YAW_ANGLE_INC_FACT;

	if (GimbalRef.pitch_angle_dynamic_ref >= PITCH_UP_LIMIT)
		GimbalRef.pitch_angle_dynamic_ref = PITCH_UP_LIMIT;
	if (GimbalRef.pitch_angle_dynamic_ref <= PITCH_DOWM_LIMIT)
		GimbalRef.pitch_angle_dynamic_ref = PITCH_DOWM_LIMIT;

	yawref_angle_last = GimbalRef.yaw_position_ref;
	GimbalRef.yaw_position_ref = mouse->x * MOUSE_TO_YAW_ANGLE_INC_FACT;
	if (GimbalRef.yaw_position_ref == 0 && yawref_angle_last != 0)
	{
		PTZ_save_flag = 1;
	}
	else if (GimbalRef.yaw_position_ref != 0 && yawref_angle_last == 0)
	{
		start_stasticflag = 0;
	}
	if (GimbalRef.yaw_position_ref == 0 && start_stasticflag == 1)
	{
		PTZ_static_flag = 1;
	}
	else
	{
		PTZ_static_flag = 0;
	}

	/* not used to control, just as a flag */
	GimbalRef.pitch_speed_ref = mouse->y; //speed_ref仅做输入量判断用
	GimbalRef.yaw_speed_ref = mouse->x;
	MouseShootControl(mouse);
}

//输入模式设置
void SetInputMode(Remote *rc)
{
	if (rc->s2 == 1)
	{
		inputmode = REMOTE_INPUT;
	}
	else if (rc->s2 == 3)
	{
		inputmode = KEY_MOUSE_INPUT;
	}
	else if (rc->s2 == 2)
	{
		inputmode = STOP;
	}
}

InputMode_e GetInputMode()
{
	return inputmode;
}

void Remote_Rx(u8 *RxMsg)
{
	RC_CtrlData.rc.ch0 = ((int16_t)RxMsg[0] | ((int16_t)RxMsg[1] << 8)) & 0x07FF;
	RC_CtrlData.rc.ch1 = (((int16_t)RxMsg[1] >> 3) | ((int16_t)RxMsg[2] << 5)) & 0x07FF;
	RC_CtrlData.rc.ch2 = (((int16_t)RxMsg[2] >> 6) | ((int16_t)RxMsg[3] << 2) |
						  ((int16_t)RxMsg[4] << 10)) &
						 0x07FF;
	RC_CtrlData.rc.ch3 = (((int16_t)RxMsg[4] >> 1) | ((int16_t)RxMsg[5] << 7)) & 0x07FF;

	RC_CtrlData.rc.s1 = ((RxMsg[5] >> 4) & 0x000C) >> 2;
	RC_CtrlData.rc.s2 = ((RxMsg[5] >> 4) & 0x0003);

	RC_CtrlData.mouse.x = ((int16_t)RxMsg[6]) | ((int16_t)RxMsg[7] << 8);
	RC_CtrlData.mouse.y = ((int16_t)RxMsg[8]) | ((int16_t)RxMsg[9] << 8);
	RC_CtrlData.mouse.z = ((int16_t)RxMsg[10]) | ((int16_t)RxMsg[11] << 8);

	RC_CtrlData.mouse.press_l = RxMsg[12];
	RC_CtrlData.mouse.press_r = RxMsg[13];

	RC_CtrlData.key.v = ((int16_t)RxMsg[14]);

	SetInputMode(&RC_CtrlData.rc);

	switch (GetInputMode())
	{
	case REMOTE_INPUT:
	{
		//遥控器控制模式
		RemoteControlProcess(&(RC_CtrlData.rc));
	}
	break;

	case KEY_MOUSE_INPUT:
	{
		//键鼠控制模式
		MouseKeyControlProcess(&RC_CtrlData.mouse, &RC_CtrlData.key);
	}
	break;
	case STOP:
	{
		Chassis_Speed_Ref_Init();
		HAL_TIM_Base_Stop_IT(&htim3);
		//紧急停车
		for (i = 0; i < 8; i++)
		{
			databuf[i] = 0;
			databuf_PTZ[i] = 0;
		}
		CAN1_Send_Msg(databuf);
		CAN1_Send_Msg_PTZ(databuf_PTZ);
	}
	break;
	}
}
void phone_Rx(u8 *RxMsg)
{

	RC_CtrlData.rc.ch1 = (((int16_t)RxMsg[1] >> 3) | ((int16_t)RxMsg[2] << 5)) & 0x07FF;
	RC_CtrlData.rc.ch2 = (((int16_t)RxMsg[2] >> 6) | ((int16_t)RxMsg[3] << 2) |
						  ((int16_t)RxMsg[4] << 10)) &
						 0x07FF;
	RC_CtrlData.rc.ch3 = (((int16_t)RxMsg[4] >> 1) | ((int16_t)RxMsg[5] << 7)) & 0x07FF;

	RC_CtrlData.rc.s1 = ((RxMsg[5] >> 4) & 0x000C) >> 2;
	RC_CtrlData.rc.s2 = ((RxMsg[5] >> 4) & 0x0003);

	RC_CtrlData.mouse.x = ((int16_t)RxMsg[6]) | ((int16_t)RxMsg[7] << 8);
	RC_CtrlData.mouse.y = ((int16_t)RxMsg[8]) | ((int16_t)RxMsg[9] << 8);
	RC_CtrlData.mouse.z = ((int16_t)RxMsg[10]) | ((int16_t)RxMsg[11] << 8);

	RC_CtrlData.mouse.press_l = RxMsg[12];
	RC_CtrlData.mouse.press_r = RxMsg[13];

	RC_CtrlData.key.v = ((int16_t)RxMsg[14]);
}
//遥控器控制模式处理
void RemoteControlProcess(Remote *rc)
{

	//        Chassis_Speed_Ref.forward_back_ref = (RC_CtrlData.rc.ch1 - (int16_t)REMOTE_CONTROLLER_STICK_OFFSET) * STICK_TO_CHASSIS_SPEED_REF_FACT;
	//        Chassis_Speed_Ref.left_right_ref   = (rc->ch0 - (int16_t)REMOTE_CONTROLLER_STICK_OFFSET) * STICK_TO_CHASSIS_SPEED_REF_FACT;
	//
	//      if(Chassis_Speed_Ref.forward_back_ref>5000)
	//			 {
	//			 Chassis_Speed_Ref.forward_back_ref=5000;
	//			 }
	//			 else if(Chassis_Speed_Ref.forward_back_ref<-5000)
	//			 {
	//			  Chassis_Speed_Ref.forward_back_ref=-5000;
	//			 }
	//
	//       if(  Chassis_Speed_Ref.left_right_ref	>5000)
	//			 {
	//			   Chassis_Speed_Ref.left_right_ref	=5000;
	//			 }
	//			 else if(  Chassis_Speed_Ref.left_right_ref	<-5000)
	//			 {
	//			    Chassis_Speed_Ref.left_right_ref	=-5000;
	//			 }
	//---------------------yaw-----------------------------------
	/*把通道返回的速度转化为角度*/
	GimbalRef.yaw_angle_dynamic_ref += (rc->ch2 - (int16_t)REMOTE_CONTROLLER_STICK_OFFSET) * STICK_TO_YAW_ANGLE_INC_FACT;
	GimbalRef.pitch_angle_dynamic_ref += (rc->ch3 - (int16_t)REMOTE_CONTROLLER_STICK_OFFSET) * STICK_TO_PITCH_ANGLE_INC_FACT;
	//	yawref_angle_last= GimbalRef.yaw_position_ref;
	//	GimbalRef.yaw_position_ref= (rc->ch2 - (int16_t)REMOTE_CONTROLLER_STICK_OFFSET) * STICK_TO_YAW_ANGLE_INC_FACT;
	///*下面的逻辑的作用是什么*/
	//	if(GimbalRef.yaw_position_ref==0&&yawref_angle_last!=0)
	//	{
	//		PTZ_save_flag=1;
	//	}
	//	else if(GimbalRef.yaw_position_ref!=0&&yawref_angle_last==0)
	//	{
	//		PTZ_save_flag=0;
	//		start_stasticflag=0;
	//	}
	//	if(GimbalRef.yaw_position_ref==0&&start_stasticflag==1)
	//	{
	//		PTZ_static_flag=1;
	//	}
	//	else
	//	{
	//		PTZ_static_flag=0;
	//	}
	/*角度限幅*/

	if (GimbalRef.yaw_angle_dynamic_ref >= YAW_UP_LIMIT)
		GimbalRef.yaw_angle_dynamic_ref = YAW_UP_LIMIT;
	if (GimbalRef.yaw_angle_dynamic_ref <= YAW_DOWM_LIMIT)
		GimbalRef.yaw_angle_dynamic_ref = YAW_DOWM_LIMIT;

	if (GimbalRef.pitch_angle_dynamic_ref >= PITCH_UP_LIMIT)
		GimbalRef.pitch_angle_dynamic_ref = PITCH_UP_LIMIT;
	if (GimbalRef.pitch_angle_dynamic_ref <= PITCH_DOWM_LIMIT)
		GimbalRef.pitch_angle_dynamic_ref = PITCH_DOWM_LIMIT;

	/* not used to control, just as a flag */

	RemoteShootControl(rc->s1);
}

//遥控器摩擦轮
void RemoteShootControl(uint8_t val)
{
	static u8 key_flag = 1;
	static u8 hit_flag = 0;
	static u16 Friction_wheel = 0;
	if (key_flag && val == 1)
	{
		key_flag = 0;
		if (Friction_wheel % 2 == 0)
		{
			TIM_SetTIM12Compare1(NORMAL_FRICITION_ON_SPEED);
			TIM_SetTIM12Compare2(NORMAL_FRICITION_ON_SPEED);
			//			   LASER_ON();
		}
		else
		{
			TIM_SetTIM12Compare1(900);
			TIM_SetTIM12Compare2(900);
			//			   LASER_OFF();
		}

		hit_flag = !hit_flag;
		Friction_wheel++;
	}

	if (val == 3)
	{
		key_flag = 1;

#if DIALPOSITIONORSPEED == 1
		position_hit += 0;
#else
		Dial_motor_speed_ref = 0;
		ShootingFreCon.Shooting_delay = 0;
#endif
	}

	if (val == 2 && hit_flag)
	{
#if DIALPOSITIONORSPEED == 1

		if (key_mouse_inf.last_val == 3 && __fabs(Dial_the_motor.ecd_angle - position_hit) < STHREEBULLET_DISTANCE)
		{

			position_hit += DIAL_THREE_POSITION;
			key_mouse_inf.shoot_flag = 1;
		}

		//			if(__fabs(Dial_the_motor.ecd_angle-position_hit)<ONEBULLET_DISTANCE)
		//	 	{
		//
		//			position_hit+=DIAL_ONE_POSITION;
		//			key_mouse_inf.shoot_flag=1;
		//
		//		}

		//		 if(Hit_position.pid_out>320&&Dial_motor_speed_fdb<REDIAL_BULLET_SPEED&&key_mouse_inf.shoot_flag)
		//			 {
		//				       position_hit=Dial_the_motor.ecd_angle-DIAL_BACK_POSITION;
		//					   key_mouse_inf.shoot_flag=0;
		//			 }

		//这是什么？

#else
		if (key_mouse_inf.last_val == 3 && ShootingFreCon.Shooting_delay == 0)
		{
			ShootingFreCon.Shooting_delay = DIAL_ONE_DELAY;
		}

		if (__fabs(Dial_motor_speed_fdb) <= 5 && Hit_speed.pid_out > 500) //如果卡子弹，倒拨一段
		{
			ShootingFreCon.Shooting_delay = 3;
			Dial_motor_speed_ref = -DIAL_SPEED;
		}

		if (ShootingFreCon.Shooting_delay)
		{
			Dial_motor_speed_ref = DIAL_SPEED;
			ShootingFreCon.Shooting_delay--;
		}
		else
		{
			Dial_motor_speed_ref = 0;
		}

#endif
	}
	key_mouse_inf.last_val = val;
}
